require "posix";


local oo = require "loop.base"

File = oo.class{ };

function File:createNewFile( pathname )
    local f = assert(io.open(pathname, "w+"));
    assert( f:flush() );
    assert( f:close() );
end 

function File:writeFile( pathname, content )
    local f = assert( io.open( pathname,'w') );
    assert( f:write( content ) );
    assert( f:flush() );
    assert( f:close() );
end;

function File:readFile( pathname )
    local f = assert( io.open( pathname, 'r' ) );
    local ret = assert( f:read('*a') );
    assert( f:close() );
    return ret
end

function File:createNewDir( pathname )
	status, errstr = posix.mkdir(pathname);
	if status == nil then
        io.stderr:write(pathname..": "..errstr.."\n");
		return false;
	end
	return true;
end

function File:exists( pathname )
	local f = io.open(pathname,"r")
   	if f~=nil then io.close(f); return true; else return false; end
end

function File:copy( pathsrc, pathdst )
	return assert( os.execute( 'cp -rf ' .. pathsrc .. ' ' .. pathdst ) )
end

function File:remove( path )
	return assert( os.execute( 'rm -rf ' .. path ) )
end

function File:isEmpty( pathdir )
	local count = -2
	for name in posix.files( pathdir ) do
		count = count + 1
		if count > 0 then
			return false
		end
	end
	return  true
end


